package com.zat.mes.andrmes.dofun.work.vm

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.love.zywanandroid.retrofit.RetrofitManager
import com.love.zywanandroid.retrofit.api.ApiService
import com.zat.mes.andrmes.data.BaseResponse
import com.zat.mes.andrmes.data.PackBean
import com.zat.mes.andrmes.data.StyleFileBean
import com.zat.mes.andrmes.retrofit.callback.MesCallBack
import okhttp3.RequestBody
import retrofit2.Call
import retrofit2.Response

/**
 * @author zy
 * @date :2020-09-23 13:46
 * @description:
 */
class HouseModel(application: Application) : AndroidViewModel(application){

    private val packData: MutableLiveData<PackBean> = MutableLiveData()
    private val postData:MutableLiveData<String> = MutableLiveData()
    private val styleData:MutableLiveData<StyleFileBean> = MutableLiveData()


    fun getPackData(): LiveData<PackBean> {
        return packData
    }
    fun getPostData():LiveData<String>{
        return postData
    }
    fun getStyleData():LiveData<StyleFileBean>{
        return styleData
    }
    /**
     * 根据卡查询包(做工)
     */
    fun loadPackDataByRFRD(rfId: String?) {
        RetrofitManager.apiService(ApiService::class.java)
            .getPackByRfidHouse(rfId)
            .enqueue(object : MesCallBack<BaseResponse<PackBean>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<PackBean>>?,
                    response: Response<BaseResponse<PackBean>>?
                ) {
                    val result = response?.body()?.result
                    packData.postValue(result)
                }

            })
    }

    /**
     * 根据二维码查询包
     */
    fun loadPackData(qrCode: String?) {
        RetrofitManager.apiService(ApiService::class.java)
            .getPackByQrcodeHouse(qrCode)
            .enqueue(object : MesCallBack<BaseResponse<PackBean>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<PackBean>>?,
                    response: Response<BaseResponse<PackBean>>?
                ) {
                    val result = response?.body()?.result
                    packData.postValue(result)
                }

            })
    }

    /**
     * 入库
     */
    fun loadInhouseSave(jsonArray: String) {
        val requestBody: RequestBody = RequestBody.create(RetrofitManager.JOSNTYPE, jsonArray)
        RetrofitManager.apiService(ApiService::class.java)
            .postHouseSave(requestBody)
            .enqueue(object : MesCallBack<BaseResponse<String>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<String>>?,
                    response: Response<BaseResponse<String>>?
                ) {
                    postData.postValue(response?.body()?.message)
                }


            })
    }
    /**
     * 查询款式资料
     */
    fun loadStyleExtraFileData(cancelEmp:String?){
        RetrofitManager.apiService(ApiService::class.java)
            .getSytelFile(cancelEmp)
            .enqueue(object : MesCallBack<BaseResponse<StyleFileBean>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<StyleFileBean>>?,
                    response: Response<BaseResponse<StyleFileBean>>?
                ) {
                    styleData.postValue(response?.body()?.result)
                }


            })
    }
}